package com.success.application.ground.holder;

import android.content.Intent;
import android.util.Log;
import android.view.View;

import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.TextHttpResponseHandler;
import com.success.application.api.YuanbaoApi;
import com.success.application.base.BaseHolder;
import com.success.application.common.param.Params;
import com.success.application.common.result.Result;
import com.success.application.ground.bean.Friend;
import com.success.application.ground.bean.RollImage;
import com.success.application.myapplication.R;
import com.success.application.widget.BannerImageLoader;
import com.success.application.widget.GsonUtils;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import org.json.JSONObject;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;
import cz.msebera.android.httpclient.Header;
import cz.msebera.android.httpclient.entity.StringEntity;

/**
 * Created by mzk on 2017/3/23.
 */

public class TopHolder extends BaseHolder<Friend> implements OnBannerListener {

    //轮播图图片路径集合
    public static List<String> images = new ArrayList<>();
    //轮播图标题集合
    public static List<String> titles = new ArrayList<>();
    //请求类型区别每个业务的请求
    private int requestType = 0;
    //星期
    private String week;

    //视图组件
    @Bind(R.id.i_banner)
    Banner mIBanner;

    public TopHolder(View v) {
        super(v);
    }

    @Override
    protected void bindItem(Friend friend) {
        try {
            mIBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
            //设置图片加载器
            mIBanner.setImageLoader(new BannerImageLoader());
            //第击每一项的banner监听
            mIBanner.setOnBannerListener(this);
            //设置banner动画效果
            mIBanner.setBannerAnimation(Transformer.Default);
            //设置自动轮播，默认为true
            mIBanner.isAutoPlay(true);
            //设置轮播时间
            mIBanner.setDelayTime(1500);
            //设置指示器位置（当banner模式中有指示器时）
            mIBanner.setIndicatorGravity(BannerConfig.CENTER);
            requestType = 0;
            //获取banner图片
            Params params = new Params();
            long time=System.currentTimeMillis();
            Date date=new Date(time);
            SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 EEEE");
            format=new SimpleDateFormat("EEEE");
            if(format.format(date)=="星期一"){
                week = "1";
            }else if(format.format(date)=="星期二"){
                week = "2";
            }else if(format.format(date)=="星期三"){
                week = "3";
            }else if(format.format(date)=="星期四"){
                week = "4";
            }else if(format.format(date)=="星期五"){
                week = "5";
            }else if(format.format(date)=="星期六"){
                week = "6";
            }else {
                week = "7";
            }
            params.put("gdtp_zsWeek", "1");
            YuanbaoApi.getBannerList(params, new IndexFragmentHandler());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 自定义加载监听器
    public class IndexFragmentHandler extends TextHttpResponseHandler {

        @Override
        public void onSuccess(int statusCode, Header[] headers, String responseString) {
            try {
                Type type = new TypeToken<Map>() {
                }.getType();
                Map result = GsonUtils.fromJson(responseString, type);
//                if (Result.RESULT_SUCCESS.equals(result.getResult()) || result.getData()!=null) {
                setData(result);
//                }
            } catch (Exception e) {
                e.printStackTrace();
                onFailure(statusCode, headers, responseString, e);
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
        }
    }

    private void setData(Map result) {
            images = new ArrayList<String>();
            titles = new ArrayList<String>();
            List list = (List) result.get("data");
            for (int i = 0; i <list.size() ; i++) {
                Map ss  = (Map)list.get(i);
                images.add(ss.get("gdtp_ptUrl").toString());
                titles.add(ss.get("gdtp_ptZt").toString());
            }
            //设置图片集合
            mIBanner.setImages(images);
            //设置标题集合（当banner样式有显示title时）
            mIBanner.setBannerTitles(titles);
            //banner设置方法全部调用完毕时最后调用
            mIBanner.start();

    }

    //滚动图点击事件，暂时写死，测试一下
    @Override
    public void OnBannerClick(int position) {
        if(position == 0){

        }
//        else if(position == 1){ //援助
//            Intent intent = new Intent(itemView.getContext(), HelpPubActivity.class);
//            itemView.getContext().startActivity(intent);
//        }
//        else if(position == 2){ //咨询
//            Intent intent = new Intent(itemView.getContext(), ConsultPubActivity.class);
//            itemView.getContext().startActivity(intent);
//        }
        else{

        }
    }
}
